package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorPermission;
import guoanmaker.operator.business.model.OperatorRole;
import guoanmaker.operator.business.model.OperatorRoleUser;
import guoanmaker.operator.business.model.OperatorUser;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.repository.OperatorPermissionRepository;
import guoanmaker.operator.business.repository.OperatorRoleRepository;
import guoanmaker.operator.business.repository.OperatorRoleUserRepository;
import guoanmaker.operator.business.repository.OperatorUserRepository;
import guoanmaker.operator.helper.common.util.StringUtil;

/**
 * 
 * @author  zj
 *
 */
@Service
public class OperatorRoleService {
	
	private final OperatorRoleRepository  operatorRoleRepository;
	
	private final OperatorPermissionRepository operatorPermissionRepository;
	
	private final OperatorRoleUserRepository operatorRoleUserRepository;
	
	private final OperatorUserRepository   operatorUserRepository;
	 
	@Autowired  
	private  HttpServletRequest request;
	
	
	@Autowired
	public  OperatorRoleService(OperatorRoleRepository  operatorRoleRepository,OperatorPermissionRepository operatorPermissionRepository,
			OperatorRoleUserRepository operatorRoleUserRepository,OperatorUserRepository   operatorUserRepository){
		this.operatorRoleRepository=operatorRoleRepository;
		this.operatorPermissionRepository=operatorPermissionRepository;
		this.operatorRoleUserRepository=operatorRoleUserRepository;
		this.operatorUserRepository=operatorUserRepository;
	}
	
	public  OperatorRoleRepository getOperatorRoleRepository(){
		return operatorRoleRepository;
	}
	
	public  OperatorPermissionRepository  getOperatorPermissionRepository(){
		return operatorPermissionRepository;
	}
	
	public OperatorRoleUserRepository getOperatorRoleUserRepository(){
		
		return operatorRoleUserRepository;
	}
	
	public OperatorUserRepository getOperatorUserRepository(){
		return operatorUserRepository;
	}
	
	public Page<OperatorRole>  operatorRoleListPage(Integer page,Integer size){
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<OperatorRole> lists=operatorRoleRepository.findAll(pageable);
		if(lists.getContent().size()>0){
			for (OperatorRole operatorRole : lists.getContent()) {
				operatorRole.setUsers(null);
			    List<OperatorUser> list=operatorUserRepository.selectByroleId(operatorRole.getId());
			    //0--->使用中  1--->未使用
			    if(list.size()>0){
			    	operatorRole.setStatus(0);
			    	operatorRoleRepository.save(operatorRole);
			    }else{
			    	operatorRole.setStatus(1);
			    	operatorRoleRepository.save(operatorRole);
			    }
			}
		}
		return lists;
	}
	public List<OperatorRole>  operatorRoleList(){
		List<OperatorRole> lists=operatorRoleRepository.findAll();
		return lists;
	}
	
	

	
	@Transactional
	public  ReturnResult addRole(String  rolename,String pids ,String userid){
		
		ReturnResult returnResult = new ReturnResult();
		
		OperatorRole role=operatorRoleRepository.selectByRolename(rolename);
		if(role!=null){
			returnResult.setKey("error");
			returnResult.setValue("角色名称被占用");
		}else{
			OperatorRole  operatorRole=new OperatorRole();
			operatorRole.setCreateTime(new Date());
			operatorRole.setRolename(rolename);
			OperatorUser operatorUser =operatorUserRepository.findOne(userid);
			if(operatorUser!=null){
				operatorRole.setCreateUserid(operatorUser.getId());
				operatorRole.setCreateUser(operatorUser.getUsername());
			}
			operatorRole.setStatus(1);
			operatorRole.setPids(pids);
			operatorRoleRepository.save(operatorRole);
			returnResult.setKey("success");
			returnResult.setValue("保存成功");
		}
		return returnResult;
		
	}
	
	public  OperatorRole  selectRoleByid(String  id){
		
		return  operatorRoleRepository.findOne(id);
	}
	
	
	
	
	@Transactional
	public ReturnResult editRole(String id,String  rolename,String pids,String userid){
         ReturnResult returnResult = new ReturnResult();
         OperatorRole operatorRole=operatorRoleRepository.findOne(id);
         if(operatorRole!=null){
        	if(operatorRole.getRolename().equals(rolename)){
    			operatorRole.setCreateTime(new Date());
    			OperatorUser operatorUser =operatorUserRepository.findOne(userid);
    			if(operatorUser!=null){
    				operatorRole.setCreateUserid(operatorUser.getId());
    				operatorRole.setCreateUser(operatorUser.getUsername());
    			}
    			operatorRole.setStatus(1);
    			operatorRole.setPids(pids);
    			operatorRoleRepository.save(operatorRole);
    			returnResult.setKey("success");
    			returnResult.setValue("编辑成功");
        	} else{
        		OperatorRole role=operatorRoleRepository.selectByRolename(rolename);
        		if(role!=null){
        			returnResult.setKey("error");
        			returnResult.setValue("角色名称被占用");
        		}else{
        			operatorRole.setCreateTime(new Date());
        			operatorRole.setRolename(rolename);
        			OperatorUser operatorUser =operatorUserRepository.findOne(userid);
        			if(operatorUser!=null){
        				operatorRole.setCreateUserid(operatorUser.getId());
        				operatorRole.setCreateUser(operatorUser.getUsername());
        			}
        			operatorRole.setStatus(1);
        			operatorRole.setPids(pids);
        			operatorRoleRepository.save(operatorRole);
        			returnResult.setKey("success");
        			returnResult.setValue("编辑成功");
        		}
        	}
         }
		
		return returnResult;
		
	}
	
	@Transactional
	public ReturnResult deleteByrole(String id){
		ReturnResult returnResult=new ReturnResult();
			operatorRoleRepository.delete(id);
			returnResult.setKey("success");
			returnResult.setValue("删除成功");
		return  returnResult;
		
	}
	
	@Transactional
	public ReturnResult deleteByRoles(String ids){
		ReturnResult returnResult = new ReturnResult();	
			String[] str=StringUtil.StrList(ids);
			List<OperatorRole>  lists=new ArrayList<>();
			String id="";
			OperatorRole operatorRole=null;
			for (int i = 0; i < str.length; i++) {
				operatorRole=new OperatorRole();
				id=str[i];
				operatorRole.setId(id);
				lists.add(operatorRole);
			}
			operatorRoleRepository.delete(lists);
			returnResult.setKey("success");
			returnResult.setValue("删除成功");
		return returnResult;
	}
	
	public  List<String> clickByopuserName(String id){
		List<OperatorRoleUser> operatorRoleUsers =operatorRoleUserRepository.selectByroleid(id);
		List<String>  accounts=new ArrayList<>();
		if(operatorRoleUsers.size()>0){
			for (OperatorRoleUser operatorRoleUser : operatorRoleUsers) {
				OperatorUser operatorUser=operatorUserRepository.findOne(operatorRoleUser.getUserid());
				if(operatorUser!=null){
					accounts.add(operatorUser.getUsername());
				}
			}
		}
		return accounts;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	

}
